package leetcode.code1653;

public class Solution1 {
	public int minimumDeletions(String s) {
		int n = s.length();
		int a[] = new int[n], b[] = new int[n];
		for (int i = 0, cnt = 0; i < n; i++) {
			cnt += s.charAt(i) == 'b' ? 1 : 0;
			b[i] = cnt;
		}
		for (int i = n - 1, cnt = 0; i > 0; i--) {
			cnt += s.charAt(i) == 'a' ? 1 : 0;
			a[i - 1] = cnt;
		}
		int ans = a[0];
		for (int i = 0; i < n; i++) {
			ans = Math.min(ans, a[i] + b[i]);
		}
		return ans;
	}
}
